package com.cc.pms.util;

import java.security.SecureRandom;
import java.util.UUID;

public class InviteCodeEncoderUtil {

    private static final String BASE62_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    private static final int BASE = BASE62_CHARS.length();
    private static final SecureRandom RANDOM = new SecureRandom();

    /**
     * 生成唯一的随机字符串
     * @param length 输出字符串的长度
     * @return 唯一的随机字符串
     */
    public static String generateRandomString(int length) {
        StringBuilder sb = new StringBuilder(length);
        // 生成指定长度的随机字符串
        for (int i = 0; i < length; i++) {
            sb.append(BASE62_CHARS.charAt(RANDOM.nextInt(BASE)));
        }
        // 结合 UUID 确保唯一性
        return sb.toString() + UUID.randomUUID().toString().replace("-", "").substring(0, Math.min(8, 32 - length));
    }

    public static void main(String[] args) {
        String randomString = InviteCodeEncoderUtil.generateRandomString(12);
        System.out.println(randomString);
    }
}
